﻿ using OAWeb.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OAWeb.Controllers
{
    public class ScoreController : Controller
    {
        private OADbContext db = new OADbContext();
        
        // GET: Score
        public ActionResult Index(int examId, int courseId = 0, int classId = 0)
        {
            ///

            List<StudentScore> scores = db.Grades.Where(g => g.ExamID == examId).Select(g => new StudentScore { StdNumber = g.StudentID, Name = g.Student.Name, ClassName = g.Student.TeachingClass.ClassName, CourseName = g.Course.Subject, Score = g.Score }).ToList();// Where(g => g.ExamID == examId).Select(g => new StudentScore { StdNumber = g.StudentID, Name = "", Score = g.Score }).ToList();
            return View(scores);
        }
        public ActionResult ScoreShow(int classId, int examId, int courseId = 0)
        {

            Student[] students = db.Students.Where(st => st.TeachingClassID == classId).ToArray();
            CourseExam[] courseExams = db.CourseExams.Where(c => c.ExamID == examId).ToArray();
            int cid = courseExams[0].CourseID;
            Course[] courses = new Course[courseExams.Length];
            for (int i = 0; i < courseExams.Length; i++)
            {
                int id = courseExams[i].CourseID;
                courses[i] = db.Courses.Single(c => c.ID == id);
            }

            //ViewBag.stu = students;
            ViewBag.cours = courses;
            
            //ViewBag.scores = Scores;
            List<StudentScore> scores = db.Grades.Where(g => g.ExamID == examId).Select(g => new StudentScore { StdNumber = g.StudentID, Number = g.Student.Number, Name = g.Student.Name, ClassName = g.Student.TeachingClass.ClassName, CourseName = g.Course.Subject, Score = g.Score }).ToList();
            // var grades = db.Grades.Where(g => g.ExamID == examId).ToList();
            Grade[] grades = db.Grades.Where(g => g.ExamID == examId & g.CourseID == cid).ToArray();
            //ViewBag.grades = grades;

            Grade[] grades1 = db.Grades.Where(g => g.ExamID == examId & g.CourseID == cid & g.Student.TeachingClassID == classId).ToArray();
           // ViewBag.grades1 = grades1;
            Dic(examId, courses);
            BLL obj = new BLL();
            obj.ScoresRand(scores, students, courses, grades, grades1);
            ViewBag.s = obj.s;
            ViewBag.stuLength = obj.stuLength;
            ViewBag.e = obj.e;
            ViewBag.ssum = obj.ssum;
            ViewBag.avg = obj.avg;
            return View(scores);

        }
        

        public ActionResult GradesShow(int examId, int courseId = 0)
        {
            int did = db.Exams.Single(e => e.ID == examId).DepartmentGradeID;
            Student[] students = db.Students.Where(s => s.TeachingClass.DepartmentGradeID == did).ToArray();
            CourseExam[] courseExams = db.CourseExams.Where(c => c.ExamID == examId).ToArray();
            int cid = courseExams[0].CourseID;
            Course[] courses = new Course[courseExams.Length];
            for (int i = 0; i < courseExams.Length; i++)
            {
                int id = courseExams[i].CourseID;
                courses[i] = db.Courses.Single(c => c.ID == id);
            }
            ViewBag.cours = courses;
            List<StudentScore> scores = db.Grades.Where(g => g.ExamID == examId).Select(g => new StudentScore { StdNumber = g.StudentID, Number = g.Student.Number, Name = g.Student.Name, ClassName = g.Student.TeachingClass.ClassName, CourseName = g.Course.Subject, Score = g.Score }).ToList();
            StudentScore[] scores1 = db.Grades.Where(g => g.ExamID == examId & g.CourseID == cid).Select(g => new StudentScore { StdNumber = g.StudentID, Number = g.Student.Number, Name = g.Student.Name, ClassName = g.Student.TeachingClass.ClassName, CourseName = g.Course.Subject, Score = g.Score }).ToArray();
            ViewBag.scores1 = scores1;
            // var grades = db.Grades.Where(g => g.ExamID == examId).ToList();
            Grade[] grades = db.Grades.Where(g => g.ExamID == examId & g.CourseID == cid).ToArray();
            ViewBag.grades = grades;
            double[,] Scores = new double[grades.Length, courses.Length];
            ViewBag.scores = Scores;

            return View(scores);
        }
        public void Dic(int examId, Course[] courses)
        {
            int did = db.Exams.Single(e => e.ID == examId).DepartmentGradeID;
            Student[] students = db.Students.Where(s1 => s1.TeachingClass.DepartmentGradeID == did).ToArray();


            List<StudentScore> scores = db.Grades.Where(g => g.ExamID == examId).Select(g => new StudentScore { StdNumber = g.StudentID, Number = g.Student.Number, Name = g.Student.Name, ClassName = g.Student.TeachingClass.ClassName, CourseName = g.Course.Subject, Score = g.Score }).ToList();
            // var grades = db.Grades.Where(g => g.ExamID == examId).ToList();
            Grade[] grades = db.Grades.Where(g => g.ExamID == examId & g.CourseID == 1).ToArray();

            double[,] Scores = new double[grades.Length, courses.Length];

            double[] s = new double[grades.Length];

            for (int k = 0; k < grades.Length; k++)
            {

                double sum = 0;
                for (int j = 0; j < courses.Length; j++)
                {

                    Scores[k, j] = scores.Single(g => g.StdNumber == grades[k].StudentID && g.CourseName == courses[j].Subject).Score;
                    sum = sum + Scores[k, j];

                }

                s[k] = sum;

            }

            Dictionary<string, double> d = new Dictionary<string, double>();


            for (int i = 0; i < s.Length; i++)
            {

                d.Add(grades[i].StudentID.ToString(), s[i]);
            }

            var result = from pair in d
                         orderby pair.Value descending
                         select pair;

            ViewBag.dict = result;
        }
        static int[] count = new int[30];
        static int cours = 0;
        public ActionResult ScoreCount1(int examId)
        {
            var departmentgradeid = db.Exams.Single(e => e.ID == examId).DepartmentGradeID;
            TeachingClass[] teachingClasses = db.TeachingClasses.Where(t => t.DepartmentGradeID == departmentgradeid).ToArray();
            ViewBag.teachingClasses = teachingClasses;
            CourseExam[] courseExams = db.CourseExams.Where(c => c.ExamID == examId).ToArray();
            int cid = courseExams[0].CourseID;
            Course[] courses = new Course[courseExams.Length];
            for (int i = 0; i < courseExams.Length; i++)
            {
                int id = courseExams[i].CourseID;
                courses[i] = db.Courses.Single(c => c.ID == id);
            }
            ViewBag.cours = courses;

            List<StudentScore> scores = db.Grades.Where(g => g.ExamID == examId).Select(g => new StudentScore { StdNumber = g.StudentID, Number = g.Student.Number, Name = g.Student.Name, ClassName = g.Student.TeachingClass.ClassName, CourseName = g.Course.Subject, Score = g.Score }).ToList();
            // var grades = db.Grades.Where(g => g.ExamID == examId).ToList();
            Grade[] grades = db.Grades.Where(g => g.ExamID == examId).ToArray();
            ViewBag.grades = grades;
            double[,] Scores = new double[grades.Length, courses.Length];
            ViewBag.scores = Scores;

            int[] countItems = new int[teachingClasses.Length * 5 * courses.Length];
            int score = 100;
            int k = -1;

            for (int i = 0; i < teachingClasses.Length; i++)
            {

                for (int c = 0; c < courses.Length; c++)
                {
                    int count = 0;
                    int l = teachingClasses[i].TeachingClassID;
                    int cous = courses[c].ID;
                    score = 100;
                    for (int j = 0; j < 5; j++)
                    {
                        k++;
                        score -= 10;


                        if (j != 4)
                        {
                            count = (from gradeItems in db.Grades
                                     where gradeItems.Student.TeachingClassID == l && gradeItems.ExamID == examId && gradeItems.CourseID == cous && gradeItems.Score >= score && gradeItems.Score < score + 10
                                     select gradeItems.StudentID).Count();

                        }
                        else
                        {
                            count = (from gradeItems in db.Grades
                                     where gradeItems.Student.TeachingClassID == l && gradeItems.ExamID == examId && gradeItems.CourseID == cous && gradeItems.Score < 60
                                     select gradeItems.StudentID).Count();
                        }
                        countItems[k] = count;


                    }
                }

            }


            ViewBag.count = countItems;
            count = countItems;
            cours = courses.Length;
            //ViewBag.count1 = countItems1;
            // ViewBag.count2 = countItems2;
            return View(grades);
        }
       
        public ActionResult ScoreCount2(int examId)
        {
            //var departmentgradeid = db.Exams.Single(e => e.ID == examId).DepartmentGradeID;
            
            List<int> classid = new List<int>();
            //获取参加这次考试的班级id
            var tid = db.Grades.Where(g => g.ExamID == examId).GroupBy(g=>g.Student.TeachingClassID).Select(g => (new { tid = g.Key,classname=g.Select(item=>item.Student.TeachingClass.ClassName), count = g.Count() }));
            List<TeachingClass[]> classname = new List<TeachingClass[]>();
            foreach (var id in tid)
            {
                classid.Add(id.tid);
                TeachingClass[] teachingClasses = db.TeachingClasses.Where(t => t.TeachingClassID == id.tid).ToArray();
               // string cname = teachingClasses[0].ClassName;
                classname.Add(teachingClasses);
            }
            
            //ViewBag.tid = classid;
            ViewBag.teachingClasses = classname;
            CourseExam[] courseExams = db.CourseExams.Where(c => c.ExamID == examId).ToArray();
            int cid = courseExams[0].CourseID;
            ViewBag.eid = examId;
            Course[] courses = new Course[courseExams.Length];
            for (int i = 0; i < courseExams.Length; i++)
            {
                int id = courseExams[i].CourseID;
                courses[i] = db.Courses.Single(c => c.ID == id);
            }
            ViewBag.cours = courses;

            List<StudentScore> scores = db.Grades.Where(g => g.ExamID == examId).Select(g => new StudentScore { StdNumber = g.StudentID, Number = g.Student.Number, Name = g.Student.Name, ClassName = g.Student.TeachingClass.ClassName, CourseName = g.Course.Subject, Score = g.Score }).ToList();
            // var grades = db.Grades.Where(g => g.ExamID == examId).ToList();
            Grade[] grades = db.Grades.Where(g => g.ExamID == examId).ToArray();
            ViewBag.grades = grades;
            double[,] Scores = new double[grades.Length, courses.Length];
            ViewBag.scores = Scores;

            double[] countItems = new double[classid.Count * 4 * courses.Length];


            int k = -1;
            for (int c = 0; c < courses.Length; c++)
            {
               
                double count = 0;

                int cous = courses[c].ID;

                for (int j = 0; j < 4; j++)
                {

                    for (int i = 0; i < classid.Count; i++)
                    {
                        k++;
                        int l = classid[i];


                        if (j == 0)
                        {
                            count = (from gradeItems in db.Grades
                                     where gradeItems.Student.TeachingClassID == l && gradeItems.ExamID == examId && gradeItems.CourseID == cous
                                     select gradeItems.Score).Average();
                            count = Math.Round(count, 2);
                        }
                        if (j == 1)
                        {
                            double num = (from gradeItems in db.Grades
                                          where gradeItems.Student.TeachingClassID == l && gradeItems.ExamID == examId && gradeItems.CourseID == cous && gradeItems.Score >= 60
                                          select gradeItems.Score).Count();

                            double Totalnum = (from gradeItems in db.Grades
                                               where gradeItems.Student.TeachingClassID == l && gradeItems.ExamID == examId && gradeItems.CourseID == cous
                                               select gradeItems.Score).Count();
                            count = Math.Round(num / Totalnum * 100, 2);
                            
                        }
                        if (j == 2)
                        {
                            double num = (from gradeItems in db.Grades
                                          where gradeItems.Student.TeachingClassID == l && gradeItems.ExamID == examId && gradeItems.CourseID == cous && gradeItems.Score >= 80
                                          select gradeItems.Score).Count();

                            double Totalnum = (from gradeItems in db.Grades
                                               where gradeItems.Student.TeachingClassID == l && gradeItems.ExamID == examId && gradeItems.CourseID == cous
                                               select gradeItems.Score).Count();
                            count = Math.Round(num / Totalnum * 100, 2);
                        }

                        if (j == 3)
                        {
                            int num = (from gradeItems in db.Grades
                                       where gradeItems.Student.TeachingClassID == l && gradeItems.ExamID == examId && gradeItems.CourseID == cous
                                       select gradeItems.Score).Count();

                            double[] scoreitem = (from gradeItems in db.Grades
                                                  where gradeItems.Student.TeachingClassID == l && gradeItems.ExamID == examId && gradeItems.CourseID == cous
                                                  orderby gradeItems.Score
                                                  select gradeItems.Score).ToArray();
                            if (num % 2 != 0)
                            {
                                count = scoreitem[num / 2];
                            }
                            else
                            {
                                count = (scoreitem[(num / 2) - 1] + scoreitem[num / 2]) / 2;
                                count = Math.Round(count, 2);
                            }

                        }
                        countItems[k] = count;


                    }
                }
                  
            }
                     
            ViewBag.count = countItems;
          
           
            return View(scores);
        }
        public ActionResult ScoreSubject(int classId, int examId, int courseId)
        {
            List<StudentScore> scores = db.Grades.Where(g => g.ExamID == examId & g.Student.TeachingClassID == classId & g.CourseID == courseId).Select(g => new StudentScore { StdNumber = g.StudentID, Number = g.Student.Number, Name = g.Student.Name, ClassName = g.Student.TeachingClass.ClassName, CourseName = g.Course.Subject, Score = g.Score }).ToList();
            Grade[] grades = db.Grades.Where(g => g.ExamID == examId & g.Student.TeachingClassID == classId & g.CourseID == courseId).ToArray();
            double[] scoreitems = new double[scores.ToArray().Length];
            for (int i = 0; i < scores.ToArray().Length; i++)
            {
                scoreitems[i] = scores.Single(s => s.StdNumber == grades[i].StudentID).Score;
            }
            ViewBag.scoreitems = scoreitems;



            Grade[] grades1 = db.Grades.Where(g => g.ExamID == examId & g.CourseID == courseId).ToArray();
            double[] scoreitems1 = new double[grades1.Length];
            for (int i = 0; i < grades1.Length; i++)
            {
                scoreitems1[i] = grades1.Single(g => g.StudentID == grades1[i].StudentID).Score;
            }

            Dictionary<string, double> d = new Dictionary<string, double>();


            for (int i = 0; i < grades1.Length; i++)
            {

                d.Add(grades1[i].StudentID.ToString(), scoreitems1[i]);
            }

            var result = from pair in d
                         orderby pair.Value descending
                         select pair;

            ViewBag.dict = result;
            return View(scores);
        }

        public ActionResult ExamChose()
        {
            List<Exam> exam = db.Exams.ToList();

            return View(exam);
        }
        public ActionResult ScoreShowc(int id)
        {
            ViewBag.examid = id;
            var exams = db.Exams.Single(g => g.ID == id);

            List<TeachingClass> teachingClasses = db.TeachingClasses.Where(t => t.DepartmentGradeID == exams.DepartmentGradeID).ToList();

            return View(teachingClasses);
        }

        public ActionResult ScoreChose(int classid, int examid)
        {
            ViewBag.classid = classid;
            ViewBag.examid = examid;

            CourseExam[] courseExams = db.CourseExams.Where(c => c.ExamID == examid).ToArray();


            int cid = courseExams[0].CourseID;
            Course[] courses = new Course[courseExams.Length];
            for (int i = 0; i < courseExams.Length; i++)
            {
                int id = courseExams[i].CourseID;
                courses[i] = db.Courses.Single(c => c.ID == id);
            }

            ViewBag.cours = courses;
            return View();
        }


        public ActionResult ScoreBar(int id)
        {
            ViewBag.id = id;
            CourseExam[] courseExams = db.CourseExams.Where(c => c.ExamID == id).ToArray();
            ViewBag.c = courseExams.Length;
            return View();
        }
        //各科各班人数统计数据
        public JsonResult getdata()         
        {
            string b = Request.Form["id"];
            string cuid = Request.Form["cid"];
            int cid2 = int.Parse(cuid);
            int id1 =int.Parse(b);
            var departmentgradeid = db.Exams.Single(e => e.ID == id1).DepartmentGradeID;
            TeachingClass[] teachingClasses = db.TeachingClasses.Where(t => t.DepartmentGradeID == departmentgradeid).ToArray();
            CourseExam[] courseExams = db.CourseExams.Where(c => c.ExamID == id1).ToArray();
            int cid = courseExams[0].CourseID;
            Course[] courses = new Course[courseExams.Length];
            for (int i = 0; i < courseExams.Length; i++)
            {
                int id2 = courseExams[i].CourseID;
                courses[i] = db.Courses.Single(c => c.ID == id2);
            }
            int score = 100;





            int count = 0;
            var count5 = new List<object>();

            for (int j = 0; j < 5; j++)
            {
                int k = 0;
                int[] countItems = new int[teachingClasses.Length];
                score -= 10;
                for (int c = 0; c < teachingClasses.Length; c++)
                {
                   

                    int l = teachingClasses[c].TeachingClassID;

                    if (j != 4)
                    {
                        count = (from gradeItems in db.Grades
                                 where gradeItems.Student.TeachingClassID == l && gradeItems.ExamID == id1 && gradeItems.CourseID == cid2 && gradeItems.Score >= score && gradeItems.Score < score + 10
                                 select gradeItems.StudentID).Count();

                    }
                    else
                    {
                        count = (from gradeItems in db.Grades
                                 where gradeItems.Student.TeachingClassID == l && gradeItems.ExamID == id1 && gradeItems.CourseID == cid2 && gradeItems.Score < 60
                                 select gradeItems.StudentID).Count();
                    }
                    countItems[k] = count;
                    k++;

                }
                count5.Add(countItems);
            }
           
            return Json(count5,JsonRequestBehavior.AllowGet);
            
        }
        public JsonResult getpassrate()
        {
            int examId = int.Parse(Request.Form["id"]);
            int j = int.Parse(Request.Form["j"]);
            var passratelist= SCcount(examId,j);
            return Json(passratelist, JsonRequestBehavior.AllowGet);

        }
        //获取要查看的统计图数据
        public List<object> SCcount(int examId,int j)
        {
            var departmentgradeid = db.Exams.Single(e => e.ID == examId).DepartmentGradeID;
            List<int> classid = new List<int>();
            //获取参加这次考试的班级id
            var tid = db.Grades.Where(g => g.ExamID == examId).GroupBy(g => g.Student.TeachingClassID).Select(g => (new { tid = g.Key, classname = g.Select(item => item.Student.TeachingClass.ClassName), count = g.Count() }));
            List<TeachingClass[]> classname = new List<TeachingClass[]>();
            foreach (var id in tid)
            {
                classid.Add(id.tid);
                TeachingClass[] teachingClasses = db.TeachingClasses.Where(t => t.TeachingClassID == id.tid).ToArray();
                // string cname = teachingClasses[0].ClassName;
                classname.Add(teachingClasses);
            }

            CourseExam[] courseExams = db.CourseExams.Where(c => c.ExamID == examId).ToArray();
            int cid = courseExams[0].CourseID;

            Course[] courses = new Course[courseExams.Length];
            for (int i = 0; i < courseExams.Length; i++)
            {
                int id = courseExams[i].CourseID;
                courses[i] = db.Courses.Single(c => c.ID == id);
            }       
            Grade[] grades = db.Grades.Where(g => g.ExamID == examId).ToArray(); 
            var passratelist = new List<object>();


            int k = -1;
            for (int c = 0; c < courses.Length; c++)
            {
                double[] passrate = new double[classid.Count];
                double count = 0;

                int cous = courses[c].ID;

               

                    for (int i = 0; i < classid.Count; i++)
                    {
                        k++;
                        int l = classid[i];


                        if (j == 0)
                        {
                            count = (from gradeItems in db.Grades
                                     where gradeItems.Student.TeachingClassID == l && gradeItems.ExamID == examId && gradeItems.CourseID == cous
                                     select gradeItems.Score).Average();
                            count = Math.Round(count, 2);
                        }
                        if (j == 1)
                        {
                            double num = (from gradeItems in db.Grades
                                          where gradeItems.Student.TeachingClassID == l && gradeItems.ExamID == examId && gradeItems.CourseID == cous && gradeItems.Score >= 60
                                          select gradeItems.Score).Count();

                            double Totalnum = (from gradeItems in db.Grades
                                               where gradeItems.Student.TeachingClassID == l && gradeItems.ExamID == examId && gradeItems.CourseID == cous
                                               select gradeItems.Score).Count();
                            count = Math.Round(num / Totalnum * 100, 2);
                            passrate[i] = count;
                        }
                        if (j == 2)
                        {
                            double num = (from gradeItems in db.Grades
                                          where gradeItems.Student.TeachingClassID == l && gradeItems.ExamID == examId && gradeItems.CourseID == cous && gradeItems.Score >= 80
                                          select gradeItems.Score).Count();

                            double Totalnum = (from gradeItems in db.Grades
                                               where gradeItems.Student.TeachingClassID == l && gradeItems.ExamID == examId && gradeItems.CourseID == cous
                                               select gradeItems.Score).Count();
                            count = Math.Round(num / Totalnum * 100, 2);
                            passrate[i] = count;
                        }

                        if (j == 3)
                        {
                            int num = (from gradeItems in db.Grades
                                       where gradeItems.Student.TeachingClassID == l && gradeItems.ExamID == examId && gradeItems.CourseID == cous
                                       select gradeItems.Score).Count();

                            double[] scoreitem = (from gradeItems in db.Grades
                                                  where gradeItems.Student.TeachingClassID == l && gradeItems.ExamID == examId && gradeItems.CourseID == cous
                                                  orderby gradeItems.Score
                                                  select gradeItems.Score).ToArray();
                            if (num % 2 != 0)
                            {
                                count = scoreitem[num / 2];
                            }
                            else
                            {
                                count = (scoreitem[(num / 2) - 1] + scoreitem[num / 2]) / 2;
                                count = Math.Round(count, 2);
                            }

                        }                      
                    
                    }
                passratelist.Add(passrate);

            }
            return passratelist;
        }
        public ActionResult ScoreBar2(int id)
        {

            ViewBag.id = id;
            //ViewBag.countItems = items;
            //ViewBag.cours = cours;
            return View();
        }

    }
}